//
//  CYCSMoreVC.m
//  CreditAddressBook
//
//  Created by mosu on 2016/9/28.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCSMoreVC.h"
#import "CYHttpTool.h"
#import "NSObject+MJKeyValue.h"
#import "CYJsonInfo.h"
#import "UIImageView+WebCache.h"
#import "MWPhoto.h"
#import "LEPhotoBrowserTool.h"


@interface CYCSMoreVC ()
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *timeLab;
@property (weak, nonatomic) IBOutlet UILabel *nameLab;
@property (weak, nonatomic) IBOutlet UILabel *sexLab;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;
@property (weak, nonatomic) IBOutlet UILabel *idcardLab;
@property (weak, nonatomic) IBOutlet UILabel *addressLab;
@property (weak, nonatomic) IBOutlet UILabel *qqLab;
@property (weak, nonatomic) IBOutlet UITextView *contentTV;
@property (nonatomic, strong) NSMutableArray *picAddress;
@property (weak, nonatomic) IBOutlet UIView *photoView;
@property (weak, nonatomic) IBOutlet UILabel *accountLab;

@end

@implementation CYCSMoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
     _picAddress = [[NSMutableArray alloc]init];
     
     _titleLab.text = _cycuishoumodel.title;
     _timeLab.text = _cycuishoumodel.createTime;
     _nameLab.text = [NSString stringWithFormat:@"姓名：%@",_cycuishoumodel.name];
     if (_cycuishoumodel.sex.intValue == 1) {
          _sexLab.text = @"性别：男";
     }else{
          _sexLab.text = @"性别：女";
     }
     _phoneLab.text = [NSString stringWithFormat:@"手机号码：%@",_cycuishoumodel.phone];
     _idcardLab.text = [NSString stringWithFormat:@"身份证号：%@",_cycuishoumodel.certificateNo];
     _addressLab.text = [NSString stringWithFormat:@"所在地区：%@",	_cycuishoumodel.homeAddress];
     if (_cycuishoumodel.defaulterQq) {
          _qqLab.text = [NSString stringWithFormat:@"QQ：%@",_cycuishoumodel.defaulterQq];
     }else{
          _qqLab.text = @"--";
     }
     _contentTV.text = _cycuishoumodel.content;
     
     [self setimagecerIDView];
    // Do any additional setup after loading the view from its nib.
}

- (void)setimagecerIDView{
     
     // image 前缀
     NSString *preFixString = @"http://www.credit-manage.com/";
     // 屏幕的宽度 (也是承载imageView的view的宽度)
     CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
     // image各个边界的间隔
     CGFloat space = 10.f;
     // imageview的width (设定为5张)
     CGFloat imgWidth = (screenWidth - space * 4) / 3;
     // imageview的height
     CGFloat imgHeight = _photoView.frame.size.height - space * 2;
     // 由于是xib + 手码, 代码获取xib控件的frame 必须在主线程中获取
     dispatch_async(dispatch_get_main_queue(), ^{
          
          [CYHttpTool postWithURL:[CYURL shared].getEvidencePhotos params:@{@"defaulterId":_cycuishoumodel.defaulterId} success:^(id json) {
               
               CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
               if (jsoninfo.isSuccess) {
                    NSArray *picAddressPic = jsoninfo.data;
                    _accountLab.text = [NSString stringWithFormat:@"(点击图片查看更多，共%lu张)",(unsigned long)picAddressPic.count];
                    for (NSDictionary *dic in picAddressPic) {
                         NSString *picAddress = [dic objectForKey:@"picAddress"];
                         [_picAddress addObject:picAddress];
                         for (int i = 0; i < _picAddress.count; i++ ) {
                              // image的url string
                              NSString *imgUrlString = [_picAddress objectAtIndex:i];
                              imgUrlString = [preFixString stringByAppendingString:imgUrlString];
                              // 创建imageView
                              UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(space + (imgWidth + space) * i, space, imgWidth, imgHeight)];
                              imgView.backgroundColor = [UIColor whiteColor];
                              // 此处你可使用SDWebImage来加载
                              [imgView sd_setImageWithURL:[NSURL URLWithString:imgUrlString]];
                              [_photoView addSubview:imgView];
                         }
                         if (_picAddress.count){
                              UITapGestureRecognizer *tag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoBrowser:)];
                              [_photoView addGestureRecognizer:tag];
                         }

                    }
                    
               }
               
          } failure:^(NSError *error) {
               
          }];

     });

     
     
}
/**
 *  图片浏览器
 */

- (void)photoBrowser:(UITapGestureRecognizer *)tag {
     NSArray *urls = _picAddress;
     MWPhoto *photo = nil;
     NSMutableArray *photos = [NSMutableArray array];
     for (NSString *url in urls) {
          photo = [MWPhoto photoWithURL:CYURLWithService(url)];
          //        photo.caption = @"title";
          [photos addObject:photo];
     }
     [LEPhotoBrowserTool showPhotoBrowserWithController:self photos:photos];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
